var course_boxes_loaded = 0;

function clearSel(sel) {
	var obj = document.getElementById(sel);
	obj.length = 0;
	addSelOption (sel, "- - Select from ... - - ", 0, false);
}

function setSelDefault(sel,value) {
	//console.log (value);
	var obj = document.getElementById(sel);
	obj.options[obj.selectedIndex].text= value;
	//obj.text = value;
}
function  addSelOption (sel, text, value, selected) {
	var obj = document.getElementById(sel);
	var oOption = document.createElement("OPTION");
	oOption.text = text;
	oOption.value = value;
	if (selected) oOption.selected = selected
	try {
		obj.add(oOption, null); // standards compliant; doesn't work in IE
	  }
	  catch(ex) {
		  obj.add(oOption); // IE only
	  }
}







function json__obj_count(obj) {
	var count = 0;
	for (_o in obj) count ++;
	return count;
}

function goProspectus(prospectus_code, start_date) {
	var ProspectusList = document.getElementById("ProspectusList");
	ProspectusList.innerHTML = "Searching for course details...";
	setAjaxParameter("ProspectusCode",prospectus_code);
	
	
	var xml = getFile("/modules/search_box/json_obtainprospectus.php");
	var json = JSON.parse(xml);
	
	
	//Print course details
	var op_text = "<div id=course_details>";
	op_text += "<h2>" + json["Details"]["Heading"] + "</h2>";
	op_text += "<div class=course_details_summary>";
		
	op_text += "<span class=strong>Level: </span><span class=normal>" + json["Details"]["Level"] + "</span>";
	op_text += "<span class=strong> Subject Area: </span><span class=normal>" + json["Details"]["SubjectArea"] + "</span>";
	op_text += "<span class=strong> Curriculum Area: </span><span class=normal>" + json["Details"]["CurriculumArea"] + "</span>";
	
	op_text += "</div>";
	
	
	var count=0;

	
	for (_d in json["Sections"]["Section"]) {
		if (json["Sections"]["Section"][_d]["Heading"] != undefined) {
			op_text += "<div class=course_details_heading>" + json["Sections"]["Section"][_d]["Heading"] + "</div>";
			//if (json__obj_count(json["Sections"]["Section"][_d]["Content"]) > 0) 
			
			
			op_text += "<div class=course_details_content>";
			
			if (typeof json["Sections"]["Section"][_d]["Content"] !== "object") op_text +=  json["Sections"]["Section"][_d]["Content"];
			else for (x in json["Sections"]["Section"][_d]["Content"]) 
				if (typeof json["Sections"]["Section"][_d]["Content"][x] !== "function") {
					if (typeof json["Sections"]["Section"][_d]["Content"][x] != "object") op_text += "<br>" + json["Sections"]["Section"][_d]["Content"][x];
					else for (w in json["Sections"]["Section"][_d]["Content"][x]) op_text +=  json["Sections"]["Section"][_d]["Content"][x][w];
				}
			//	if (typeof json["Sections"]["Section"][_d]["Content"][x] != "object") op_text += json["Sections"]["Section"][_d]["Content"][x];
				//else for (w in json["Sections"]["Section"][_d]["Content"][x]) op_text += json["Sections"]["Section"][_d]["Content"][x][w];
			//}
			
			
			
			/*
			for (var i=0; i<json__obj_count(json["Sections"]["Section"][_d]["Content"]); i++) {
				if (typeof json["Sections"]["Section"][_d]["Content"][i] == "object") {
					op_text += "<div class=sub_details>"
					for (var j=0; j< json__obj_count(json["Sections"]["Section"][_d]["Content"][i]);j++) {
						if (json["Sections"]["Section"][_d]["Content"][i][j] != undefined)
							op_text += "<div class=sub_details_content>" + json["Sections"]["Section"][_d]["Content"][i][j] + "</div>";
					}
					op_text += "</div>";
				} else 
					if ( json["Sections"]["Section"][_d]["Content"][i] != undefined) op_text +=  json["Sections"]["Section"][_d]["Content"][i];
				
			}
			*/
			op_text +=  "</div>";
			
			
		}
	}
	
	
	
	op_text += "</div>";
	
	if (json["LearningAims"] != undefined) {
	//Print Coasts
	op_text += "<div id=course_aims>";
	
	
	op_text += 	"<div class=aim_title>";
	if (json["LearningAims"]["Programme"] != undefined) {
		op_text += "<div class=aim_title_left>" + json["LearningAims"]["Programme"]["Title"] + "</div>";
		op_text +=	"<div class=aim_title_right>" + json["LearningAims"]["Programme"]["Code"] + "</div>";
	}
	op_text += 	"<div class=aim_details_heading>";
	op_text += 		"<div class=aim_cost>Fees*</div>";
	op_text += 	"</div>";
	
	//alert(json["LearningAims"]["Programme"]["TotalFee"]);
	if (json["LearningAims"]["Programme"] != undefined) {
	op_text += 	"<div class=aim_details>";
	op_text += 		"<div class=aim_cost>" + json["LearningAims"]["Programme"]["TotalFee"] + "</div>";
	op_text += 	"</div>";
	}
	
	op_text +=	"</div>";
	
	
	//Print learning aims
	for (_a in json["LearningAims"]["Aim"]) {
		if (json["LearningAims"]["Aim"][_a]["Title"] != undefined ) {
			op_text += "<div class=aim>";
			op_text += 	"<div class=aim_title>";
			op_text +=		"<div class=aim_title_left>" + json["LearningAims"]["Aim"][_a]["Title"] + "</div>";
			
			op_text +=		"<div class=aim_title_right>";
			if (typeof  json["LearningAims"]["Aim"][_a]["Code"] != "object" ) op_text +=  json["LearningAims"]["Aim"][_a]["Code"];
			else for (_c in json["LearningAims"]["Aim"][_a]["Code"]) op_text += json["LearningAims"]["Aim"][_a]["Code"][_c]; 
			op_text += "</div>";
						
			op_text +=	"</div>";
			op_text += 	"<div class=aim_details_heading>";
			op_text += 		"<div class=aim_weeks>Start date</div>";
			op_text += 		"<div class=aim_weeks>Number of Weeks</div>";
			op_text += 		"<div class=aim_attend>Attendance</div>";
			op_text += 		"<div class=aim_cost>Fees</div>";
			op_text += 	"</div>";
			op_text += 	"<div class=aim_details>";
			op_text += 		"<div class=aim_weeks>" + start_date  + "</div>";
			op_text += 		"<div class=aim_weeks>"  + json["LearningAims"]["Aim"][_a]["NumberOfWeeks"] + "</div>";
			
			op_text += 		"<div class=aim_attend>";
			if (typeof  json["LearningAims"]["Aim"][_a]["Attendance"] != "object" ) op_text +=  json["LearningAims"]["Aim"][_a]["Attendance"];
			else for (_sa in json["LearningAims"]["Aim"][_a]["Attendance"]) op_text +=  json["LearningAims"]["Aim"][_a]["Attendance"][_sa];
			
			op_text += "</div>";
			
			op_text += 		"<div class=aim_cost>" + json["LearningAims"]["Aim"][_a]["TotalFee"] + "</div>";
			op_text += 	"</div>";
			op_text += "</div>";
		}
	}
	op_text += "</div>";
	
	
   op_text += "<div id=course_aims>";
	
	op_text += 	"<div class=aim_details>";
	
	op_text += 	"<div class=aim_details style='clear:both'>";
	op_text += 		"<div class=aim_cost>*Reduced Fees </div>";
	if (json["LearningAims"]["Programme"] != undefined)  op_text += "<div class=aim_cost_statement>" + json["LearningAims"]["Programme"]["ReducedFee"] + "</div>";
	op_text += 		"<div class=aim_cost_statement> (Please contact the Information Centre for eligibility rules)</div>";
	op_text += 	"</div>";
	}
	op_text +=	"</div>";
	
	//Check for bullet points
	op_text = escape(op_text).replace(/%u2022/g,"<li>");
	op_text = unescape(op_text);
	//console.log(op_text);
	
	ProspectusList.innerHTML = op_text;
	set_height();
	window.scroll(0,0);
}

